package com.iss.umservice.dao.impl;

import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.stereotype.Repository;

import com.iss.umservice.bean.AnnouncementBean;
import com.iss.umservice.bean.AnnouncementItemBean;
import com.iss.umservice.bean.QueryAnnouncementBean;
import com.iss.umservice.dao.AnnouncementDao;

@Repository
public class AnnouncementDaoImpl extends SqlMapClientDaoSupport implements
		AnnouncementDao {

	@Override
	public int addAnnouncement(AnnouncementBean newAnn) {
		int id = (Integer) getSqlMapClientTemplate().insert(
				"announcement.add_announcement", newAnn);
		for (AnnouncementItemBean t : newAnn.getReceiptItems()) {
			t.setPid(id);
			getSqlMapClientTemplate().insert(
					"announcementitem.add_announcementitem", t);
		}
		return id;
	}

	@Override
	public void modAnnouncement(AnnouncementBean ann) {
		//
		getSqlMapClientTemplate().update("announcement.modify_announcement",
				ann);
		//
		{
			AnnouncementItemBean t = new AnnouncementItemBean();
			t.setPid(ann.getId());
			getSqlMapClientTemplate().delete(
					"announcementitem.delete_announcementitem", t);
		}
		//
		for (AnnouncementItemBean t : ann.getReceiptItems()) {
			t.setPid(ann.getId());
			getSqlMapClientTemplate().insert(
					"announcementitem.add_announcementitem", t);
		}

	}

	@Override
	public void delAnnouncement(AnnouncementBean ann) {
		AnnouncementItemBean t = new AnnouncementItemBean();
		t.setPid(ann.getId());
		getSqlMapClientTemplate().delete(
				"announcementitem.delete_announcementitem", t);
		getSqlMapClientTemplate().delete("announcement.delete_announcement",
				ann);
	}

	@Override
	public List<QueryAnnouncementBean> queryAnnouncements(
			QueryAnnouncementBean condition, int offset, int row) {
		return getSqlMapClientTemplate().queryForList(
				"announcement.query_queryAnnouncementByCoditions", condition,
				offset, row);
	}

	@Override
	public AnnouncementBean getAnnouncement(AnnouncementBean condition) {
		AnnouncementBean re = (AnnouncementBean) getSqlMapClientTemplate()
				.queryForObject("announcement.get_announcementByUnique",
						condition);
		AnnouncementItemBean t = new AnnouncementItemBean();
		t.setPid(re.getId());
		re.setReceiptItems(getSqlMapClientTemplate().queryForList("announcementitem.query_announcementitemByCoditions", t));
		return re;
	}

	@Override
	public List<QueryAnnouncementBean> allAnnouncement() {
		return getSqlMapClientTemplate().queryForList(
				"announcement.all_queryAnnouncement");
	}

	@Override
	public int countAnnouncement(QueryAnnouncementBean condition) {
		return (Integer) getSqlMapClientTemplate()
				.queryForObject(
						"announcement.get_queryAnnouncementCountByCoditions",
						condition);
	}

	@Override
	public void discardAnnouncement(int id) {
		getSqlMapClientTemplate().update("announcement.discard_announcement",
				id);
	}

}
